package com.ewanghuiju.app.widget.popupwindow;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.PintuanGoodsSpecsResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.ui.pintuan.adapter.FlexboxAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.makeramen.roundedimageview.RoundedImageView;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 购买  参团弹框
 * @time 2020/2/19
 */
public class PintuanBuyCantuanPopup extends BasePopupWindow {


    @BindView(R.id.popup_close)
    ImageView popupClose;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_selected)
    TextView tvSelected;
    @BindView(R.id.popup_sure)
    TextView popupSure;
    @BindView(R.id.tv_classify_one)
    TextView tvClassifyOne;
    @BindView(R.id.tv_classify_two)
    TextView tvClassifyTwo;
    @BindView(R.id.riv_shop_img)
    RoundedImageView rivShopImg;
    @BindView(R.id.rv_color)
    RecyclerView rvColor;
    @BindView(R.id.rv_size)
    RecyclerView rvSize;

    private List<PintuanGoodsSpecsResponBean> mList;

    private PintuanBuyCantuanCallback pintuanBuyCantuanCallback;

    public PintuanBuyCantuanPopup(Context context, PintuanShopListResponBean pintuanShopListResponBean, PintuanBuyCantuanCallback pintuanBuyCantuanCallback) {
        super(context);
//        AutoSize.cancelAdapt((Activity) context);
        setWidth(getScreenWidth());
        ButterKnife.bind(this, getContentView());
        this.pintuanBuyCantuanCallback = pintuanBuyCantuanCallback;

        if (pintuanShopListResponBean != null) {
            tvPrice.setText(pintuanShopListResponBean.getSpe_price());
            ImageLoader.load(getContext(), pintuanShopListResponBean.getImage(), rivShopImg);
        }
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_pintuan_buy_cantuan);
    }

    public void setClassifyOne(List<PintuanGoodsSpecsResponBean> list) {
        this.mList = list;
        if (list != null && list.size() > 0) {
            //2，增加谷歌流式布局
            reSetFlexbox(list);
            list.get(0).setSelected(true);
            tvClassifyOne.setVisibility(View.VISIBLE);
            rvColor.setVisibility(View.VISIBLE);
            tvClassifyOne.setText(list.get(0).getName());
            FlexboxLayoutManager manager = new FlexboxLayoutManager(getContext(), FlexDirection.ROW, FlexWrap.WRAP) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            rvColor.setLayoutManager(manager);
            FlexboxAdapter mFlexboxAdapter = new FlexboxAdapter(R.layout.item_popup_buycantuan_flowlayout, 1);
            mFlexboxAdapter.setNewData(list);
            rvColor.setAdapter(mFlexboxAdapter);
            mFlexboxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    PintuanGoodsSpecsResponBean mPintuanGoodsSpecsResponBean = (PintuanGoodsSpecsResponBean) adapter.getItem(position);
                    if (mPintuanGoodsSpecsResponBean.isSelected()) {
                        return;
                    }
                    reSetFlexbox(adapter.getData());
                    mPintuanGoodsSpecsResponBean.setSelected(true);
                    mFlexboxAdapter.notifyDataSetChanged();
                    List<PintuanGoodsSpecsResponBean> pintuanGoodsSpecsResponBeans = mPintuanGoodsSpecsResponBean.getAttribute();
                    if (pintuanGoodsSpecsResponBeans == null) {
                        pintuanGoodsSpecsResponBeans = new ArrayList<>();
                    }
                    setClassifyTwo(pintuanGoodsSpecsResponBeans);
                }
            });

            //更新第二个
            List<PintuanGoodsSpecsResponBean> mAttributeList = list.get(0).getAttribute();
            if (mAttributeList == null) {
                mAttributeList = new ArrayList<>();
            }
            setClassifyTwo(mAttributeList);
        }
    }

    private FlexboxAdapter mSecondFlexboxAdapter;

    public void setClassifyTwo(List<PintuanGoodsSpecsResponBean> mSecondlist) {

        if (mSecondlist.size() == 0) {
            rvSize.setVisibility(View.INVISIBLE);
            tvClassifyTwo.setVisibility(View.INVISIBLE);
            return;
        }
        reSetFlexbox(mSecondlist);
        mSecondlist.get(0).setSelected(true);
        rvSize.setVisibility(View.VISIBLE);
        tvClassifyTwo.setVisibility(View.VISIBLE);
        tvClassifyTwo.setText(mSecondlist.get(0).getAttribute_name());
        if (mSecondFlexboxAdapter != null) {
            mSecondFlexboxAdapter.setNewData(mSecondlist);
            getFlexboxSelected();
            return;
        }
        FlexboxLayoutManager manager = new FlexboxLayoutManager(getContext(), FlexDirection.ROW, FlexWrap.WRAP) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rvSize.setLayoutManager(manager);
        mSecondFlexboxAdapter = new FlexboxAdapter(R.layout.item_popup_buycantuan_flowlayout, 2);
        mSecondFlexboxAdapter.setNewData(mSecondlist);
        rvSize.setAdapter(mSecondFlexboxAdapter);
        mSecondFlexboxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PintuanGoodsSpecsResponBean mPintuanGoodsSpecsResponBean = (PintuanGoodsSpecsResponBean) adapter.getItem(position);
                if (mPintuanGoodsSpecsResponBean.isSelected()) {
                    return;
                }
                reSetFlexbox(adapter.getData());
                mPintuanGoodsSpecsResponBean.setSelected(true);
                mSecondFlexboxAdapter.notifyDataSetChanged();
                getFlexboxSelected();
            }
        });
        getFlexboxSelected();
    }

    /**
     * 全部重置
     *
     * @param list
     */
    public void reSetFlexbox(List<PintuanGoodsSpecsResponBean> list) {
        if (list != null && list.size() > 0) {
            for (PintuanGoodsSpecsResponBean pgb : list) {
                pgb.setSelected(false);
            }
        }
    }

    private String selectedFText;
    private String selectedSText;
    private String selectedPrice;
    private int spaceId = -1;

    public void getFlexboxSelected() {
        selectedFText = "";
        selectedSText = "";
        if (mList != null && mList.size() > 0) {
            for (PintuanGoodsSpecsResponBean pgb : mList) {
                if (pgb.isSelected()) {
                    selectedFText = pgb.getName_value();
                    selectedSText = pgb.getName() + ":" + pgb.getName_value();
                    List<PintuanGoodsSpecsResponBean> msList = pgb.getAttribute();
                    if (msList != null && msList.size() > 0) {
                        for (PintuanGoodsSpecsResponBean pintuanGoodsSpecsResponBean : msList) {
                            if (pintuanGoodsSpecsResponBean.isSelected()) {
                                selectedFText = selectedFText + " " + pintuanGoodsSpecsResponBean.getValue();
                                selectedSText = selectedSText + " " + pintuanGoodsSpecsResponBean.getAttribute_name() + ":" + pintuanGoodsSpecsResponBean.getValue();
                                selectedPrice = pintuanGoodsSpecsResponBean.getSell_price();
                                spaceId = pintuanGoodsSpecsResponBean.getId();
                                break;
                            }
                        }
                    }
                }
            }
        }
        tvPrice.setText(TextUtils.isEmpty(selectedPrice) ? "" : selectedPrice);
        tvSelected.setText("已选：" + selectedFText);
    }

    @OnClick({R.id.popup_close, R.id.popup_sure})
    public void doClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.popup_close:
                dismiss();
                break;
            case R.id.popup_sure:
                //确定按钮
                if (mList != null && mList.size() > 0) {
                    if (spaceId < 0) {
                        StyleableToast.makeText(getContext(), "请选择规格", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                }
                if (pintuanBuyCantuanCallback != null) {
                    pintuanBuyCantuanCallback.sureClickCallback(spaceId, selectedSText, selectedPrice);
                }
                break;
            default:
                break;
        }
    }

    public interface PintuanBuyCantuanCallback {
        void sureClickCallback(int id, String text, String price);
    }
}
